package com.jerome.fs.config;

import com.jerome.fs.util.ReflectUtil;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 负责处理main方法传入的参数信息
 */
public class ConfigUtil {
    /**
     * 将main 方法中的args 转换为 map
     * @param args
     * @return
     */
    public static Map<String,String> getMap(String[] args){
        Map<String, String> map = new HashMap<>();
        for (String arg : args) {
            String[] keyValue = arg.split("=", 2);
            if (keyValue.length < 2) {
                System.out.println("Invalid argument format: " + arg);
                continue;
            }
            String key = keyValue[0].trim();
            String value = keyValue[1].trim();
            map.put(key,value);
            System.out.println("Key: " + key + ", Value: " + value);
        }
        return map;
    }

    /**
     * properties 转换为对应实体对象
     * @param properties
     * @param tClass
     * @param <T>
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static <T> T build(Properties properties,Class<T> tClass) throws InstantiationException, IllegalAccessException {
        Map<String, String> map = new HashMap<>();
        for (String key : properties.stringPropertyNames()) {
            String value = properties.getProperty(key);
            map.put(key, value);
        }
        return ReflectUtil.build(map,tClass);
    }

}
